﻿using System.Windows.Forms;
using DesignerHelper.Addons;
using SpreadsheetGear;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    public partial class WorkbookOptions : UserControl, IMarine<IWorkbook>
    {
        //internal event SettingRequestHandler SettingCommit;
        //internal event ResetRequestHandler ResetCommit;

        public WorkbookOptions()
        {
            InitializeComponent();
        }

        public WorkbookOptions(IWorkbook workbook) : this()
        {
            //AddHandler();
            //TriggerInit(workbook);
        }

        private IWorkbook ActiveBook { get; set; }
        private IWorkbookWindowInfo Info { get; set; }
        private const string Password = "password";

        private void AddHandler()
        {
            //SettingCommit += new SettingRequestHandler(TriggerSet);
            //ResetCommit += new ResetRequestHandler(TriggerInit);
        }

        void IMarine<IWorkbook>.TriggerInit(IWorkbook newWorkbook)
        {
            ActiveBook = newWorkbook;
            Info = ActiveBook.WindowInfo;
            Init();
        }

        void IScv<IWorkbook>.TriggerReset()
        {
            InitGroupBoxCalculation();
            InitGroupBoxWinOption();
            InitGroupBoxProtection();
            InitGroupBoxShapes();
        }

        void IScv<IWorkbook>.TriggerSet()
        {
            SetGroupBoxCalculation(); //bug 无法更改非当前激活的工作簿的显示选项
            SetGroupBoxWinOption();
            SetGroupBoxProtection();
            SetGroupBoxShapes();
        }

        Control IScv<IWorkbook>.GetControl()
        {
            return this;
        }

        #region 封装变量

        public bool PrecisionAsDisplayed
        {
            get { return checkBoxCalAsDisp.Checked; }
            private set { checkBoxCalAsDisp.Checked = value; }
        }

        public bool SaveLinkValues
        {
            get { return checkBoxSaveLinkValues.Checked; }
            private set { checkBoxSaveLinkValues.Checked = value; }
        }

        public bool ProtectStructure
        {
            get { return checkBoxProtectStructure.Checked; }
            private set { checkBoxProtectStructure.Checked = value; }
        }

        public bool FilePassword
        {
            get { return checkBoxFilePassword.Checked; }
            private set { checkBoxFilePassword.Checked = value; }
        }

        public bool DisplayWorkbookTabs
        {
            get { return checkBoxDpBookTab.Checked; }
            private set { checkBoxDpBookTab.Checked = value; }
        }

        public bool DisplayHorizontalScrollBar
        {
            get { return checkBoxDpHorScrollBar.Checked; }
            private set { checkBoxDpHorScrollBar.Checked = value; }
        }

        public bool DisplayVerticalScrollBar
        {
            get { return checkBoxDPVecScrollBar.Checked; }
            private set { checkBoxDPVecScrollBar.Checked = value; }
        }

        #endregion

        #region InitGroupBox

        private void Init()
        {
            InitGroupBoxCalculation();
            InitGroupBoxWinOption();
            InitGroupBoxProtection();
            InitGroupBoxShapes();
        }

        private void InitGroupBoxCalculation()
        {
            PrecisionAsDisplayed = ActiveBook.PrecisionAsDisplayed;
            SaveLinkValues = ActiveBook.SaveLinkValues;
        }

        private void InitGroupBoxWinOption()
        {
            DisplayWorkbookTabs = Info.DisplayWorkbookTabs;
            DisplayHorizontalScrollBar = Info.DisplayHorizontalScrollBar;
            DisplayVerticalScrollBar = Info.DisplayVerticalScrollBar;
        }

        //ToDo 完善FilePassword相关
        //
        private void InitGroupBoxProtection()
        {
            ProtectStructure = ActiveBook.ProtectStructure;
            FilePassword = ActiveBook.ProtectWindows;
        }

        private void InitGroupBoxShapes()
        {
            switch (ActiveBook.DisplayDrawingObjects)
            {
                case DisplayDrawingObjects.DisplayShapes:
                    radioButtonShowShape.Checked = true;
                    break;
                case DisplayDrawingObjects.Hide:
                    radioButtonHideShape.Checked = true;
                    break;
                case DisplayDrawingObjects.Placeholders:
                    radioButtonShowPlaceHolder.Checked = true;
                    break;
            }
        }

        #endregion

        #region SetGroupBox

        private void SetAll()
        {
            SetGroupBoxCalculation(); //bug 无法更改非当前激活的工作簿的显示选项
            SetGroupBoxWinOption();
            SetGroupBoxProtection();
            SetGroupBoxShapes();
        }

        private void SetGroupBoxCalculation()
        {
            ActiveBook.PrecisionAsDisplayed = checkBoxCalAsDisp.Checked;
            ActiveBook.SaveLinkValues = checkBoxSaveLinkValues.Checked;
        }

        private void SetGroupBoxWinOption()
        {
            Info.DisplayWorkbookTabs = DisplayWorkbookTabs;
            Info.DisplayHorizontalScrollBar = DisplayHorizontalScrollBar;
            Info.DisplayVerticalScrollBar = DisplayVerticalScrollBar;
        }

        private void SetGroupBoxProtection()
        {
            if (checkBoxProtectStructure.Checked && !ActiveBook.ProtectStructure)
            {
                ActiveBook.ProtectStructure = true;
                ActiveBook.Protect(Password, true, ActiveBook.ProtectWindows);
            }
            else if (!checkBoxProtectStructure.Checked && ActiveBook.ProtectStructure)
            {
                ActiveBook.ProtectStructure = false;
                ActiveBook.Unprotect(Password);
            }

            if (checkBoxFilePassword.Checked && !ActiveBook.ProtectWindows)
            {
                ActiveBook.ProtectWindows = true;
                ActiveBook.Protect(Password, ActiveBook.ProtectStructure, true);
            }
            else if (!checkBoxFilePassword.Checked && ActiveBook.ProtectWindows)
            {
                ActiveBook.ProtectWindows = false;
                ActiveBook.Unprotect(Password);
            }
        }

        private void SetGroupBoxShapes()
        {
            if (radioButtonShowShape.Checked)
                ActiveBook.DisplayDrawingObjects = DisplayDrawingObjects.DisplayShapes;
            else if (radioButtonHideShape.Checked)
                ActiveBook.DisplayDrawingObjects = DisplayDrawingObjects.Hide;
            else if (radioButtonShowPlaceHolder.Checked)
                ActiveBook.DisplayDrawingObjects = DisplayDrawingObjects.Placeholders;
        }

        #endregion
    }
}